﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DBUtility;
using Model;
using MySql.Data.MySqlClient;

namespace DAL
{
    public  class DDestination
    {
        public bool Exists(string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tdestination");
            strSql.Append(" where Name=@Name");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Name", MySqlDbType.VarChar,30)
          };
            parameters[0].Value = name;
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                return false;
            }
            int result = int.Parse(ds.Tables[0].Rows[0][0].ToString());
            return result > 0;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public DestinationInfo Add(Model.DestinationInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tdestination(");
            strSql.Append("Name,Lat,Lng,Type)");
            strSql.Append(" values (");
            strSql.Append("@Name,@Lat,@Lng,@Type)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Name", MySqlDbType.VarChar,30),				 
					new MySqlParameter("@Lat", MySqlDbType.Double),
                    new MySqlParameter("@Lng", MySqlDbType.Double),
                    new MySqlParameter("@Type", MySqlDbType.Int32)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Lat;
            parameters[2].Value = model.Lng;
            parameters[3].Value = model.Type;
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
               return  GetModelByName(model.Name);
            }
            else
            {
                return null;
            }
        }
        

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tdestination ");
            strSql.Append(" where Id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
};
            parameters[0].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.DestinationInfo GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Name,Lat,Lng,Type from tdestination ");
            strSql.Append(" where Id=@id");
            MySqlParameter[] parameters = {
					new MySqlParameter("@id", MySqlDbType.Int32)
            };
            parameters[0].Value = id;
            DestinationInfo model = new DestinationInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Type"] != null && ds.Tables[0].Rows[0]["Type"].ToString() != "")
                {
                    model.Type = int.Parse(ds.Tables[0].Rows[0]["Type"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Lat"] != null && ds.Tables[0].Rows[0]["Lat"].ToString() != "")
                {
                    model.Lat = double.Parse(ds.Tables[0].Rows[0]["Lat"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Lng"] != null && ds.Tables[0].Rows[0]["Lng"].ToString() != "")
                {
                    model.Lng = double.Parse(ds.Tables[0].Rows[0]["Lng"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DestinationInfo GetModelByName(string name)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Name,Lat,Lng,Type from tdestination ");
            strSql.Append(" where Name=@Name");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Name", MySqlDbType.VarChar)
            };
            parameters[0].Value = name;
            DestinationInfo model = new DestinationInfo();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Name"] != null && ds.Tables[0].Rows[0]["Name"].ToString() != "")
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Lat"] != null && ds.Tables[0].Rows[0]["Lat"].ToString() != "")
                {
                    model.Lat = double.Parse(ds.Tables[0].Rows[0]["Lat"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Lng"] != null && ds.Tables[0].Rows[0]["Lng"].ToString() != "")
                {
                    model.Lng = double.Parse(ds.Tables[0].Rows[0]["Lng"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Type"] != null && ds.Tables[0].Rows[0]["Type"].ToString() != "")
                {
                    model.Type = int.Parse(ds.Tables[0].Rows[0]["Type"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Name,Lat,Lng,Type ");
            strSql.Append(" FROM tdestination ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }
 
    }
}
